import { Injectable } from '@nestjs/common';
import { CreateLocalfileDto } from './dto/create-localfile.dto';
import { UpdateLocalfileDto } from './dto/update-localfile.dto';
import * as fs from 'fs';
import * as path from 'path';

@Injectable()
export class LocalfileService {
  private staticDir = path.join(__dirname, '../assests'); // 静态文件目录路径

  // 获取所有静态文件
  async getFile(): Promise<string[]> {
    try {
      const files = await fs.promises.readdir(this.staticDir);
      return files;
    } catch (error) {
      throw new Error(`Failed to read directory: ${error.message}`);
    }
  }
  async getFiles(folder: string): Promise<string[]> {
    try {
      const fullPath = path.join(this.staticDir, folder);
      const files = await fs.promises.readdir(fullPath);
      return files;
    } catch (error) {
      throw new Error(`Failed to read directory: ${error.message}`);
    }
  }
  getFilePath(folder: string, filename: string): string {
    return path.join(this.staticDir, folder, filename);
  }
  

  create(createLocalfileDto: CreateLocalfileDto) {
    return 'This action adds a new localfile';
  }

  findAll() {
    return `This action returns all localfile`;
  }

  findOne(id: number) {
    return `This action returns a #${id} localfile`;
  }

  update(id: number, updateLocalfileDto: UpdateLocalfileDto) {
    return `This action updates a #${id} localfile`;
  }

  remove(id: number) {
    return `This action removes a #${id} localfile`;
  }
}
